package com.mf.ergate.controller.cust;

import com.mf.ergate.api.cust.CustomerAddressRemoteService;
import com.mf.ergate.api.cust.model.dto.CustomerAddressCreateDto;
import com.mf.ergate.api.cust.model.dto.CustomerAddressDeleteDto;
import com.mf.ergate.api.cust.model.dto.CustomerAddressQueryDto;
import com.mf.ergate.api.cust.model.dto.CustomerAddressUpdateDto;
import com.mf.ergate.api.cust.model.vo.CustomerAddressVo;
import com.mf.ergate.common.utils.MfBeanUtils;
import com.mf.ergate.domain.model.cust.CustomerAddressEntity;
import com.mf.ergate.service.cust.CustomerAddressService;
import com.mf.framework.common.model.Page;
import com.mf.framework.common.model.RemotePage;
import com.mf.framework.common.model.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 客户地址薄Controller
 * User: zhaoming
 * DateTime: 2019-01-27
 * To change this template use File | Settings | File Templates.
 */
@RestController
public class CustomerAddressController implements CustomerAddressRemoteService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private CustomerAddressService customerAddressService;

    /**
     * 新增
     * @param customerAddressCreateDto 客户地址薄新增Dto
     * @return 客户地址薄
     */
    public Result<CustomerAddressVo> insert(@RequestBody CustomerAddressCreateDto customerAddressCreateDto) {
        CustomerAddressEntity customerAddressEntity = MfBeanUtils.copyObject(customerAddressCreateDto, CustomerAddressEntity.class);
        customerAddressEntity = customerAddressService.insert(customerAddressEntity);
        CustomerAddressVo customerAddressVo = MfBeanUtils.copyObject(customerAddressEntity, CustomerAddressVo.class);
        return new Result<>(customerAddressVo);
    }

    /**
     * 根据唯一编号更新
     * @param customerAddressUpdateDto 客户地址薄更新Dto
     * @return 执行结果
     */
    public Result<Boolean> updateByUniqueNo(@RequestBody CustomerAddressUpdateDto customerAddressUpdateDto) {
        CustomerAddressEntity customerAddressEntity = MfBeanUtils.copyObject(customerAddressUpdateDto, CustomerAddressEntity.class);
        Boolean flag = customerAddressService.updateByUniqueNo(customerAddressEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号删除(逻辑删除)
     * @param customerAddressDeleteDto 客户地址薄删除Dto
     * @return 结果集
     */
    public Result<Boolean> deleteByUniqueNo(@RequestBody CustomerAddressDeleteDto customerAddressDeleteDto){
        CustomerAddressEntity customerAddressEntity = MfBeanUtils.copyObject(customerAddressDeleteDto, CustomerAddressEntity.class);
        Boolean flag = customerAddressService.deleteByUniqueNo(customerAddressEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号查询
     * @param uniqueNo 唯一编号
     * @return 客户地址薄
     */
    public Result<CustomerAddressVo> selectByUniqueNo(@RequestBody String uniqueNo) {
        CustomerAddressEntity customerAddressEntity = customerAddressService.selectByUniqueNo(uniqueNo);
        CustomerAddressVo customerAddressVo = MfBeanUtils.copyObject(customerAddressEntity, CustomerAddressVo.class);
        return new Result<>(customerAddressVo);
    }

    /**
     * 根据条件查询
     * @param customerAddressQueryDto 客户地址薄查询Dto
     * @return 客户地址薄列表
     */
    public Result<List<CustomerAddressVo>> selectByCondition(@RequestBody CustomerAddressQueryDto customerAddressQueryDto) {
        List<CustomerAddressEntity> customerAddressEntities = customerAddressService.selectByCondition(customerAddressQueryDto);
        List<CustomerAddressVo> customerAddressVos = MfBeanUtils.copyList(customerAddressEntities, CustomerAddressVo.class);
        return new Result<>(customerAddressVos);
    }

    /**
     * 根据条件查询数量
     * @param customerAddressQueryDto 客户地址薄查询Dto
     * @return 数量
     */
    public Result<Integer> selectCount(@RequestBody CustomerAddressQueryDto customerAddressQueryDto) {
        return new Result<>(customerAddressService.selectCount(customerAddressQueryDto));
    }

    /**
     * 根据条件分页查询
     * @param remotePage 远程分页对象
     * @return 客户地址薄分页列表
     */
    public Result<Page<CustomerAddressVo>> selectPage(@RequestBody RemotePage<CustomerAddressQueryDto> remotePage){
        Page<CustomerAddressEntity> customerAddressEntityPage = customerAddressService.selectPage(remotePage.getPage(), remotePage.getObj());
        Page<CustomerAddressVo> page = MfBeanUtils.copyPage(customerAddressEntityPage, CustomerAddressVo.class);
        return new Result<>(page);
    }

}